package com.dtx.qiku.goods.dao.impl;

import com.dtx.qiku.annotation.Component;
import com.dtx.qiku.goods.dao.GoodsDao;
import com.dtx.qiku.goods.entity.Goods;
import com.dtx.qiku.store.FileStoreUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * 完成商品的增删改查，将数据持久化（存入数据库/文件中）
 */
@Component
public class GoodsDaoImpl implements GoodsDao {

    private static final String TABLE_NAME = "goods";

    @Override
    public void saveGoods(Goods goods) throws Exception {
        FileStoreUtils.insert(TABLE_NAME,goods,Goods.class);

    }

    @Override
    public Goods findGoodsByNum(String num) throws Exception {
        return FileStoreUtils.selectOne(TABLE_NAME,g -> Objects.equals(num,g.getNum()),Goods.class);
    }

    @Override
    public Goods findGoods() {
        return new Goods("111","goodsName",new BigDecimal("33.3"),"goodsDesc",new Date());
    }
}
